import {createRouter, createWebHashHistory, RouteRecordRaw} from 'vue-router'

const routes:Array<RouteRecordRaw> = [
  {
    path: '/',
    redirect: "/home"
  },
  {
    path: '/home',
    name: 'home',
    component: () => import("../views/index.vue"),
    children: [
      {
        path: 'spaceHome/:spaceId',
        name: 'spaceHome',
        component: () => import("../views/spaceHome/spaceHome.vue")
      }
    ]
  },
  {
    path: '/showSpace',
    name: 'showSpace',
    component: () => import("../views/showSpace/showSpace.vue")
  },
  {
    path: '/checkSpace/:spaceId',
    name: 'checkSpace',
    component: () => import("../views/checkSpace/checkSpace.vue")
  },
  {
    path: '/createSpace',
    name: 'createSpace',
    component: () => import("../views/createSpace/createSpace.vue")
  },
  {
    path: '/login',
    name: 'login',
    component: () => import("../views/login/login.vue")
  },
  {
    path: '/register',
    name: 'register',
    component: () => import("../views/register/register.vue")
  },
  {
    path: '/person/:user_id',
    name: 'person',
    component: () => import("../views/person/person.vue")
  },
  {
    path: '/message',
    name: 'message',
    component: () => import("../views/message/message.vue"),
    children: [
      {
        path: 'myMessage/:user_id',
        name: 'myMessage',
        component: () => import("../views/message/myMessage.vue")
      },
      {
        path: 'reply',
        name: 'reply',
        component: () => import("../views/message/reply.vue")
      },
      {
        path: 'getLike',
        name: 'getLike',
        component: () => import("../views/message/getLike.vue")
      },
    ]
  },
  {
    path: '/spaceConfig/:space_id',
    name: 'spaceConfig',
    component: () => import("../views/spaceConfig/spaceConfig.vue")
  },
  {
    path: '/shareSpace',
    name: 'shareSpace',
    component: () => import("../views/shareSpace/shareSpace.vue")
  },
  {
    path: '/error',
    name: 'error',
    component: () => import("../views/error.vue")
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})



router.beforeEach((to,from,next) => {
  // 路由拦截
  if(to.path != "/login" && to.path != "/register" && to.path != "/shareSpace" && to.path != "/error"){
    if(localStorage.getItem('token')){
      next()
    }else{
      from.path // 防止报错写的
      next('/login')
    }
  }else{
    next()
  }
})

export default router
